package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.mapper.MallBooksMapper;
import com.starlink.model.domain.*;
import com.starlink.model.util.SchoolbagGroupBagConvert;
import com.starlink.model.vo.SchoolbagGroupBagVO;
import com.starlink.service.SchoolbagGroupBagBooksService;
import com.starlink.service.SchoolbagGroupBagService;
import com.starlink.service.SchoolbagGroupCodeService;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagGroupDtoMapper;
import com.starlink.mapper.SchoolbagGroupMapper;
import com.starlink.model.request.SchoolbagGroupRequest;
import com.starlink.service.SchoolbagGroupService;
import com.starlink.model.util.SchoolbagGroupConvert;
import com.starlink.model.vo.SchoolbagGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;
import java.util.Objects;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagGroupServiceImpl extends ServiceImpl<SchoolbagGroupMapper, SchoolbagGroupDO> implements SchoolbagGroupService {
    @Autowired
    private SchoolbagGroupMapper schoolbagGroupMapper;
    @Autowired
    private SchoolbagGroupBagService schoolbagGroupBagService;
    @Autowired
    private SchoolbagGroupCodeService schoolbagGroupCodeService;

    @Override
    public SchoolbagGroupVO getByKey(long id) {
        SchoolbagGroupVO schoolbagGroupVO = SchoolbagGroupDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagGroupVO;
    }

    @Override
    public PageInfo<SchoolbagGroupVO> pageQuery(Pageable pageable,SchoolbagGroupRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagGroupDO> list = schoolbagGroupMapper.listQuery(request);
        List<SchoolbagGroupVO> schoolbagGroupVOList = SchoolbagGroupConvert.buildSchoolbagGroupVoList(list);
        // 分组
        for (SchoolbagGroupVO item : schoolbagGroupVOList) {
            List<SchoolbagGroupBagDO> schoolbagGroupBagList = schoolbagGroupBagService.lambdaQuery()
                    .eq(SchoolbagGroupBagDO::getCategoryId, item.getCategoryId())
                    .eq(SchoolbagGroupBagDO::getGroupId, item.getId()).list();
            List<SchoolbagGroupBagVO> schoolbagGroupBagVOS = SchoolbagGroupBagConvert.buildSchoolbagGroupBagVoList(schoolbagGroupBagList);
            item.setSchoolbagList(schoolbagGroupBagVOS);
        }
        return new PageInfo(schoolbagGroupVOList);
    }

    @Override
    public List<SchoolbagGroupVO> listQuery(SchoolbagGroupRequest request) {
        List<SchoolbagGroupDO> list = schoolbagGroupMapper.selectDOList(SchoolbagGroupDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagGroupVO> schoolbagGroupVOS = SchoolbagGroupConvert.buildSchoolbagGroupVoList(list);
        return schoolbagGroupVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagGroupRequest request) {
        save(SchoolbagGroupDtoMapper.INSTANCE.request2DO(request));
        // 设置
        SchoolbagGroupCodeDO one = schoolbagGroupCodeService.lambdaQuery().eq(SchoolbagGroupCodeDO::getType, request.getType()).last("limit 1").one();
        one.setCurrent(one.getCurrent() + 1);
        schoolbagGroupCodeService.saveOrUpdate(one);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagGroupRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagGroupDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        for (String id : Convert.toStrArray(ids)) {
            Long count = schoolbagGroupBagService.lambdaQuery().eq(SchoolbagGroupBagDO::getGroupId, id).count();
            AssertUtil.assertTrue(Objects.equals(count, 0L), "该分组下有书包，不能删除");
        }
        schoolbagGroupMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
